//声发射 算法特征值
package com.safety.algorithm.eigenvalue.soundEmission;

import com.safety.algorithm.model.soundEmission.SoundEmissionAlgModel;
import com.safety.algorithm.model.soundEmission.SoundEmissionDeviationModel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SoundEmissionAlgorithm {
    private double devSEH = 0; //偏离异常持续时间

    //声发射趋势预警指标,传入v1,v2需要的数据
    public double warnings(double v1, double v2, double dt) {
        return Math.abs(v1 - v2) / dt;
    }

    //计算m×3个数据的强度平均值
    public double average(List<SoundEmissionAlgModel> lists) {
        double sum = 0;
        for (SoundEmissionAlgModel list : lists) {
            sum += list.getMonitoringValue();
        }
        return sum / lists.size();
    }

    //n小时偏差值，n最好可以选择，默认12小时,传入n小时的数据
    public List<SoundEmissionDeviationModel> Dev(List<SoundEmissionAlgModel> lists, double averageValue) {
        List<SoundEmissionDeviationModel> result = new ArrayList<SoundEmissionDeviationModel>();
        int len = lists.size();
        double enAvg = 0;//n小时平均值
        double de;//偏差值
        for (SoundEmissionAlgModel list : lists) {
            enAvg += list.getMonitoringValue();
        }
        enAvg /= len;
        de = averageValue / enAvg - 1;
        SoundEmissionDeviationModel soundEmissionDeviationModel =
                new SoundEmissionDeviationModel(lists.get(len - 1).getDatetime(), lists.get(len - 1).getEntId(), de);
        result.add(soundEmissionDeviationModel);
        return result;
    }

    //偏离异常持续时间h，阈值为a，a默认值0.5,返回1为+m，返回0为归零
    public static List<Map<String, Object>> devAbnormal(List<Map<String, Object>> lists, int count, int m) {
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        double devHT = 0;
        int num = 0;//记录连续加的有多长
        for (Map<String, Object> map : lists) {
            if (map.get("flag").toString().equals("1")) {
                num++;
                if (num <= count) {
                    devHT += m;
                }
            } else {
                devHT = 0;
                num = 0;
            }
            map.put("dateTime", devHT);
            result.add(map);
        }
        return result;
    }

    //n小时偏离异常累计时间H 传入n小时的数据
    public static List<Map<String, Object>> devAbnormalTotal(List<Map<String, Object>> lists, int count, int m) {
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        double devHT = 0;
        int num = 0;
        for (int i = 0; i < lists.size(); i++) {
            num++;
            if (lists.get(i - count).get("flag").toString().equals("1") && num > count) {
                devHT -= m;
            }
            if (lists.get(i).get("flag").toString().equals("1")) {
                devHT += m;
            }
            lists.get(i).put("value", devHT);
            result.add(lists.get(i));
        }
        return result;
    }


    //n小时偏离异常频次N 传入n小时数据
    public static List<Map<String, Object>> devAbnormalFrequency(List<Map<String, Object>> lists, int count) {
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        double devHT = 0;
        int num = 0;
        for (int i = 0; i < lists.size(); i++) {
            num++;
            if (lists.get(i - count).get("flag").toString().equals("1") && num > count) {
                devHT--;
            }
            if (lists.get(i).get("flag").toString().equals("1")) {
                devHT++;
            }
            lists.get(i).put("value", devHT);
            result.add(lists.get(i));
        }
        return result;
    }

}
